<template>
  <view>
    <!-- 搜索框 -->
    <view class="serach-f-box"><my-search @click="gotoSearch"></my-search></view>
    <!-- 顶部轮播图 -->
    <swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="'/subpages/goods_detail/goods_detail?goods_id=' + item.goods_id"><image :src="item.image_src"></image></navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="catenav">
      <view class="catenav-item" v-for="(item, index) in navList" :key="index" @click="targetTo(item)"><image :src="item.image_src" mode=""></image></view>
    </view>
    <!-- 楼层内容 -->
    <view class="floor-box">
      <view class="floor-box-item" v-for="(item, index) in floorList" :key="index">
        <view class="floor-box-item-title"><image :src="item.floor_title.image_src"></image></view>
        <view class="floor-box-item-content">
          <view class="content-left">
            <navigator :style="{ width: item.product_list[0].image_width + 'rpx' }" :url="item.product_list[0].url">
              <image :src="item.product_list[0].image_src" mode=""></image>
            </navigator>
          </view>
          <view class="content-right">
            <navigator
              class="right-item"
              v-for="(item1, index1) in item.product_list"
              :key="index1"
              v-if="index1 !== 0"
              :style="{ width: item1.image_width + 'rpx' }"
              :url="item1.url"
            >
              <image :src="item1.image_src" mode=""></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import tabBarBadge from '@/mixins/tabBar-badge.js';
export default {
  mixins: [tabBarBadge],
  data() {
    return {
      swiperList: [], //轮播图数据
      navList: [], //分类导航数据
      floorList: [] //楼层数据
    };
  },
  methods: {
    async getSwiperListData() {
      const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata');
      console.log(res);
      if (res.meta.status !== 200) return uni.$showMsg();
      uni.$showMsg('数据加载成功!');
      this.swiperList = res.message;
    },
    async getnavListData() {
      const { data: res } = await uni.$http.get('/api/public/v1/home/catitems');
      console.log(res);
      if (res.meta.status !== 200) return uni.$showMsg();
      uni.$showMsg('数据加载成功!');
      this.navList = res.message;
    },
    targetTo(item) {
      if (item.name === '分类') {
        uni.switchTab({
          url: '../cate/cate'
        });
      }
    },
    async getFloorListData() {
      const { data: res } = await uni.$http.get('/api/public/v1/home/floordata');
      console.log(res);
      if (res.meta.status !== 200) return uni.$showMsg();
      uni.$showMsg('数据加载成功!');
      res.message.forEach(item => {
        item.product_list.forEach(item1 => {
          item1.url = '/subpages/goods_list/goods_list?' + item1.navigator_url.split('?')[1];
        });
      });
      this.floorList = res.message;
    },
    gotoSearch() {
      uni.navigateTo({
        url: '../../subpages/search/search'
      });
    }
  },
  onLoad() {
    this.getSwiperListData();
    this.getnavListData();
    this.getFloorListData();
  }
};
</script>

<style lang="scss">
.swiper {
  height: 330rpx;
  .swiper-item {
    width: 100%;
    height: 100%;
    image {
      width: 100%;
      height: 100%;
    }
  }
}

.catenav {
  display: flex;
  justify-content: space-around;
  margin: 15rpx 0;
  .catenav-item {
    width: 128rpx;
    height: 140rpx;
    image {
      width: 100%;
      height: 100%;
    }
  }
}

.floor-box {
  padding: 15rpx;
  .floor-box-item {
    margin-top: 20rpx;
    .floor-box-item-title {
      height: 60rpx;
      image {
        height: 100%;
        width: 100%;
      }
    }
    .floor-box-item-content {
      display: flex;
      justify-content: space-around;
      .content-left {
        image {
          width: 100%;
          height: 480rpx;
        }
      }
      .content-right {
        display: flex;
        justify-content: space-around;
        align-content: center;
        flex-wrap: wrap;
        .right-item {
          image {
            width: 100%;
            height: 236rpx;
          }
        }
      }
    }
  }
}

.serach-f-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
</style>
